package com.atlin.project.service.impl;

import com.atlin.project.contant.CommonConstant;
import com.atlin.project.mapper.cds.FileMetadataMapper;
import com.atlin.project.model.domain.cds.FileMetadata;
import com.atlin.project.model.dto.fileMetadata.FileMetadataQueryRequest;
import com.atlin.project.model.vo.cds.FileMetadataVO;
import com.atlin.project.service.FileMetadataService;
import com.atlin.project.utils.sql.SqlUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atlin
 * @description fileMetadata
 * @createDate date
 */
@Service
public class FileMetadataServiceImpl extends ServiceImpl<FileMetadataMapper, FileMetadata>
        implements FileMetadataService {

    @Override
    public void valid(FileMetadata fileMetadata, boolean add) {

    }

    @Override
    public FileMetadataVO getFileMetadataVO(FileMetadata fileMetadata, HttpServletRequest request) {
        FileMetadataVO fileMetadataVO = FileMetadataVO.objToVo(fileMetadata);
        // todo other operation
        return fileMetadataVO;
    }

    /**
     * 获取查询包装类（用户根据哪些字段查询，根据前端传来的请求对象，得到 mybatis 框架支持的查询 QueryWrapper 类）
     *
     * @param fileMetadataQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<FileMetadata> getQueryWrapper(FileMetadataQueryRequest fileMetadataQueryRequest) {
        QueryWrapper<FileMetadata> queryWrapper = new QueryWrapper<>();
        if (fileMetadataQueryRequest == null) {
            return queryWrapper;
        }
        Long id = fileMetadataQueryRequest.getId();
//        String title = fileMetadataQueryRequest.getTitle();
//        String content = fileMetadataQueryRequest.getContent();
        String sortField = fileMetadataQueryRequest.getSortField();
        String sortOrder = fileMetadataQueryRequest.getSortOrder();

//        queryWrapper.like(StringUtils.isNotBlank(title), "title", title);
//        queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);

        // todo other operation
        return queryWrapper;
    }

    @Override
    public Page<FileMetadataVO> getFileMetadataVOPage(Page<FileMetadata> fileMetadataPage, HttpServletRequest request) {
        List<FileMetadata> fileMetadataList = fileMetadataPage.getRecords();
        Page<FileMetadataVO> fileMetadataVOPage = new Page<>(fileMetadataPage.getCurrent(), fileMetadataPage.getSize(), fileMetadataPage.getTotal());
        if (CollectionUtils.isEmpty(fileMetadataList)) {
            return fileMetadataVOPage;
        }
        // todo other operation

        // 填充信息
        List<FileMetadataVO> fileMetadataVOList = fileMetadataList.stream().map(fileMetadata -> {
            FileMetadataVO fileMetadataVO = FileMetadataVO.objToVo(fileMetadata);
            // todo other operation

            return fileMetadataVO;
        }).collect(Collectors.toList());
        fileMetadataVOPage.setRecords(fileMetadataVOList);
        return fileMetadataVOPage;
    }
}




